package com.fhz.roomtest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.room.DatabaseConfiguration
import androidx.room.InvalidationTracker
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteOpenHelper
import java.sql.Types.NULL

class MainActivity : AppCompatActivity() {

    private val mTv: TextView by lazy { findViewById<TextView>(R.id.tv) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val db = Room.databaseBuilder(
            applicationContext,
            AppDatabase::class.java, "database-name"
        ).allowMainThreadQueries()
//            .fallbackToDestructiveMigration()
            .addMigrations(AppDatabase.MIGRATION_2_3)
            .build()

        val userDao = db.userDao()

        val user = User(NULL, "xiao", "ming","天津")

        userDao.insert(user)

        println(userDao.getAll().size)


        val studentDao = db.studentDao()
        val student = Student(NULL, "大虾", "adf")

        studentDao.insert(student)

//        println(studentDao.getStudent().size)
        
        studentDao.getStudent().observe(this){ it ->
            it.forEach {
                println(it.sName)
            }
        }

        mTv.setOnClickListener {
            studentDao.insert(student)
        }


//    }
}


}
